package com.xliu.cs.openproject.lianliankan;


import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.xliu.cs.openproject.lianliankan.music.Music;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BarCode {
    private static final Logger LOG = LoggerFactory.getLogger(BarCode.class);
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    /** 生成图像 */
    public static BufferedImage encode(String content, int width, int height, String logoPath) {
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix;// 生成矩阵
        BufferedImage bImg = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            width = bitMatrix.getWidth();
            height = bitMatrix.getHeight();
            bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    bImg.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
                }
            }

            Graphics2D gs = bImg.createGraphics();

            //载入logo
            Image img = ImageIO.read(new File(logoPath)).getScaledInstance(80, 80, Image.SCALE_SMOOTH);

            gs.drawImage(img, 110, 110, null);
            gs.dispose();
            img.flush();
            return bImg;
        } catch (Exception e) {
            LOG.error("generate bar code error", e);
        }
        return bImg;
    }

    /**
     * save image to path with certain format like 'png'
     */
    public static void saveImageToPath(BufferedImage bImage, String format, String path) {
        try {
            ImageIO.write(bImage, format, new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析图像
     */
    public static void decode(String filePath) {
        BufferedImage image;
        try {
            image = ImageIO.read(new File(filePath));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            // 对图像进行解码
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);
            String content = result.getText();

            System.out.println("图片中内容：  ");
            System.out.println(content);
            System.out.println("图片中格式：  ");
            System.out.println("encode： " + result.getBarcodeFormat());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
